<?php

class Subscriber_group_model extends MY_Model{
	
    public function __construct(){

        parent::__construct();
    }
	
	private function head()
	{
		return $this->db
			->select("
				SQL_CALC_FOUND_ROWS sg.*,
				(SELECT COUNT(id) FROM _subscriber WHERE group_id = sg.id) AS num_subscriber
				", false)
			->from('subscriber_group AS sg');
    }
	
	public function add($data = array())
	{
        $this->db->insert('subscriber_group', $data);
    }
	
	public function get($options = array())
	{
		$this->head();
		
		// WHERE
		if(isset($options['id']))
			$this->db->where('sg.id', $options['id']);

		if(isset($options['user_id']))
			$this->db->where('sg.user_id', $options['user_id']);
		
		$q = $this->db->get();

		if($q->num_rows()) return $q->row();

		return FALSE;
    }
		
	public function gets($options = array(), $limit = 0, $offset = 0)
	{
		$this->head();
		
		// WHERE
		if(isset($options['user_id']))
			$this->db->where('sg.user_id', $options['user_id']);
		
        return parent::my_result($limit, $offset);
    }
	
	public function edit($data = array(), $options = array())
	{
        $this->db->update('subscriber_group', $data, $options);
    }

	public function delete($options = array())
	{
		if($this->db->delete('subscriber_group', $options))
		{	
			// guruba ait aboneler siliniyor.
            $this->load->model('subscriber_model');
            $this->subscriber_model->delete(array('group_id' => $options['id']));
        }
    }
}

/* End of file subscriber_group_model.php */
